package com.xuzz.xrpc.spring.context.processors;

import com.xuzz.xrpc.common.ConfigManager;
import com.xuzz.xrpc.common.PropertyConfig;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.*;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * notice : EnviromentAware是由在refresh->prepareBeanFactory中注册特殊的BeanPostProcessor:ApplicationContextAwareProcessor执行的。
 * 它实际上个BeanPostProcessor，执行顺序是在BeanFactoryPostProcessor后面的。
 * 所以，ServiceAnnotationBeanPostProcessor(BeanDefinitionRegistryPostProcessor)中用不到EnviromentAware中设置的属性，
 */

/**
 * @description:
 * @author: xuzz
 * @create: 2020-02-22 18:41
 */
public class PropertyConfigAware  implements EnvironmentAware {
    @Override
    public void setEnvironment(Environment environment) {
        MutablePropertySources propertyValues =  ((ConfigurableEnvironment)environment).getPropertySources();

        Map<String,Object> xrpcProperties = new HashMap<>();
        propertyValues.forEach(pv->{
            PropertySource propertySource = propertyValues.get(pv.getName());
            if (propertySource instanceof MapPropertySource){
                String[] propertyNames = ((MapPropertySource) propertySource).getPropertyNames();
                for (int i=0,length = propertyNames.length;i<length;i++){
                    if(!propertyNames[i].startsWith("xrpc")){continue;}
                    xrpcProperties.put(propertyNames[i],propertySource.getProperty(propertyNames[i]));
                }
            }
        });
        initPropertyConfig(xrpcProperties);
    }

    private void initPropertyConfig(Map<String,Object> properties)
    {
        PropertyConfig propertyConfig = ConfigManager.propertyConfig;

        String serverName = properties.get("xrpc.server.name").toString();
        if(!StringUtils.isEmpty(serverName))
        {
            propertyConfig.setServerName(serverName);
        }

        //server address -> ip:port
        String serverAddress = properties.get("xrpc.server.address").toString();
        if(!StringUtils.isEmpty(serverAddress))
        {
            propertyConfig.setServerAddress(serverAddress);
        }
        else
        {
            propertyConfig.setServerAddress("127.0.0.1:12888");
        }

        //registry type ->
        String registryProtocol = properties.get("xrpc.registry.protocol").toString();
        if(!StringUtils.isEmpty(registryProtocol))
        {
            propertyConfig.setRegistryProtocol(registryProtocol);
        }
        else
        {
            propertyConfig.setRegistryProtocol("zookeeper");
        }

        //registry address -> ip:port
        String registryAddress = properties.get("xrpc.registry.address").toString();
        if(!StringUtils.isEmpty(registryAddress))
        {
            propertyConfig.setRegistryAddress(registryAddress);
        }
        else
        {
            propertyConfig.setRegistryAddress("127.0.0.1:2181");
        }

        String loadBalance = properties.get("xrpc.cluster.loadbalance").toString();
        if(!StringUtils.isEmpty(registryAddress))
        {
            propertyConfig.setLoadBalance(loadBalance);
        }
        else
        {
            propertyConfig.setLoadBalance("random");
        }
    }
}
